package com.fh.utils;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.core.io.PathResource;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

@Component
@ConfigurationProperties(prefix = "utils.picture")
@Data
public class IpAndPostUtisl {

    private final WebServerApplicationContext context;


    @Autowired
    public IpAndPostUtisl(WebServerApplicationContext context) {
        this.context = context;
    }

    private String address;

    private String pictureAddress;

    public String getIpAndPost()  {
        InetAddress localHost = null;
        try {
            localHost = InetAddress.getLocalHost();
        } catch(UnknownHostException e) {
            throw new RuntimeException(e);
        }
        String hostAddress = localHost.getHostAddress();
        int port = context.getWebServer().getPort();
        /* image/headpicture/ 我的本意是不想加上去的 但是
        我想如果不在这里写死的话 就需要在
        */
        return "http://" + hostAddress + ":" + port + pictureAddress;
    }

    public String getImgPath() {
        PathResource resource = new PathResource("static/image/headpicture");
        String imagePath = null;
        try {
            String path = System.getProperty("user.dir");
            imagePath = path + address + (resource.getPath().replace("\\","/"));
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
        return imagePath;
    }



}
